/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.activity;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.Terminal;
import com.boxin.ims.modules.momarketing.service.TerminalService;
import com.boxin.ims.modules.wechat.entity.Lottery;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.activity.ActivityConfig;
import com.boxin.ims.modules.wechat.service.LotteryService;
import com.boxin.ims.modules.wechat.service.WeChatService;
import com.boxin.ims.modules.wechat.service.activity.ActivityConfigService;
import com.boxin.ims.modules.wechat.utils.WechatInterfaceUtils;
import com.boxin.ims.modules.wechat.utils.WechatMenuUtils;

/**
 * 业务活动模型Controller
 * @author XiaoQian
 * @version 2014-04-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/activity/activityConfig")
public class ActivityConfigController extends BaseController {

	@Autowired
	private ActivityConfigService activityConfigService;
	
	@Autowired
	private TerminalService terminalService;
	
	@Autowired
	private WeChatService weChatService;
	
	@Autowired
	private LotteryService lotteryService;
	
	@ModelAttribute
	public ActivityConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return activityConfigService.get(id);
		}else{
			return new ActivityConfig();
		}
	}
	
	@RequiresPermissions("wechat:activity:activityConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(ActivityConfig activityConfig, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			activityConfig.setUser(user);
		}
        Page<ActivityConfig> page = activityConfigService.find(new Page<ActivityConfig>(request, response), activityConfig); 
        
       
        model.addAttribute("page", page);
        
        
		return "modules/wechat/activity/activityConfigList";
	}

	@RequiresPermissions("wechat:activity:activityConfig:view")
	@RequestMapping(value = "form")
	public String form(ActivityConfig activityConfig, Model model) throws Exception {
	
		User user = UserUtils.getUser();
		if (activityConfig.getUser() == null || activityConfig.getUser().getId() <1){
			activityConfig.setUser(user);
		}
		
		
//		 WeChat weChat=weChatService.getWeChatByUserId(UserUtils.getUser().getId());
//	        String accessToken=WechatMenuUtils.getAccessToken(weChat.getAppId(), weChat.getAppSecret());
//	        List<Map<String, Object>> groupNameList= WechatInterfaceUtils.getWeChatGroupName(accessToken);
	        
	        Terminal terminal =new Terminal();
	        terminal.setUser(UserUtils.getUser());
	        List<Terminal> terminalList= terminalService.find(terminal);
	        
	        List<Lottery>  lotteryList=  lotteryService.getActivityLotteryByUserId(UserUtils.getUser().getId());
	        
		model.addAttribute("terminalList", terminalList);
		model.addAttribute("lotteryList", lotteryList);
//        model.addAttribute("groupNameList", groupNameList);
        
		model.addAttribute("activityConfig", activityConfig);
		return "modules/wechat/activity/activityConfigForm";
	}

	@RequiresPermissions("wechat:activity:activityConfig:edit")
	@RequestMapping(value = "save")
	public String save(ActivityConfig activityConfig, Model model, RedirectAttributes redirectAttributes) throws Exception {
		if (!beanValidator(model, activityConfig)){
			return form(activityConfig, model);
		}
		activityConfigService.save(activityConfig);
		addMessage(redirectAttributes, "保存业务活动模型'" + activityConfig.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/activity/activityConfig/?repage";
	}
	
	@RequiresPermissions("wechat:activity:activityConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		activityConfigService.delete(id);
		addMessage(redirectAttributes, "删除业务活动模型成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/activity/activityConfig/?repage";
	}
	
	
}
